﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Xml.Linq;
using MyFilter;
using System.Linq.Expressions;

namespace FilesFilter
{
    /// <summary>
    /// Логика взаимодействия для Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //List<string> properties = new List<string>() { "Name", "Length" };
            List<PropertyInfo> properties = new List<PropertyInfo>() { new PropertyInfo("Name", "File Name"), new PropertyInfo("Length", "File Size") };

            if (File.Exists("filter.xml"))
            {
                _Filter = new Filter<FileInfo>(XElement.Load("filter.xml"), properties);                
            }
            else
                _Filter = new Filter<FileInfo>(false, properties);
            Filter.Filter = _Filter;
        }

        //FilterControl Filter;
        Filter<FileInfo> _Filter;
        
        private IEnumerable<FileInfo> CheckDirectory(string directory)
        {
            string[] files;
            try
            {
                files = Directory.GetFiles(directory);
            }
            catch
            {
                files = new string[0];
            }            

            foreach (string file in files)
            {
                yield return new FileInfo(file);               
            }

            string[] directories;
            try
            {
                directories = System.IO.Directory.GetDirectories(directory);
            }
            catch
            {
                directories = new string[0];
            }

            foreach (string dir in directories)
            {
                foreach (FileInfo fi in CheckDirectory(dir))
                    yield return fi;
            }            
        }              

        private void btLookUp_Click(object sender, RoutedEventArgs e)
        {
            //System.Diagnostics.Process.Start(DirName.Text);

            Expression<Func<FileInfo, bool>> exp = _Filter.BuildExpression();
            if (exp == null)
                return;

            Func<FileInfo, bool> func = exp.Compile();            

            StatDataGrid.ItemsSource =
                (from f in CheckDirectory(DirName.Text)
                 where func(f) select f).ToList<FileInfo>();
            StatDataGrid.Items.Refresh();
        }

        private void btQueryText_Click(object sender, RoutedEventArgs e)
        {
            QueryTextRTB.Document.Blocks.Clear();
            QueryTextRTB.AppendText(Filter.Filter.QueryText());
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {            
            _Filter.GetXMLRoot().Save("filter.xml");
        }

    }
}
